package com.program.art.refactor;

import java.util.stream.Collectors;

/**
 * OrderReceipt prints the details of order including customer name, address, description, quantity,
 * price and amount. It also calculates the sales tax @ 10% and prints as part
 * of order. It computes the total order amount (amount of individual lineItems +
 * total sales tax) and prints it.
 * 
 */
public class OrderReceipt {
    private Order order;

    public OrderReceipt(Order order) {
        this.order = order;
	}

	public String printReceipt() {
		return new StringBuilder()
				.append("====== Order Receipt ======\n")
				.append(order.getCustomerName() + order.getCustomerAddress())
				.append("\n ------ Products ------\n")
				.append(printLineItems())
				.append('\n' + "Sales Tax" + '\t' + order.getTotalSalesTax() + '\n')
				.append("Total Amount" + '\t' + order.getTotalAmount())
				.toString();
	}

	private String printLineItems() {
		return order.getLineItems().stream().map(OrderReceipt::printLineItem)
				.collect(Collectors.joining());
	}

	private static String printLineItem(LineItem lineItem) {
    	return new StringBuilder()
				.append(lineItem.getDescription())
				.append('\t')
				.append(lineItem.getPrice())
				.append('\t')
				.append(lineItem.getQuantity())
				.append('\t')
				.append(lineItem.totalAmount())
				.append('\n')
				.toString();
	}
}